//
//  CDGetEmailCodeStep.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/25.
//



import Combine

struct CDGetVerifyCodeStep: LoginStep {
    struct InputData {
        let account: String
        let type: RequestCodeType
    }
    
    private var cdRequestCodeStep =  CDGetSmsCodeStep()
    private var cdGetEmailCodeStep =  CDGetEmailCodeStep()
    
    func execute(_ input: InputData) -> AnyPublisher<Void, Error> {
        if input.account.isEmail {
            return cdGetEmailCodeStep.execute(.init(email: input.account, type: input.type))
                .map { _ in () }  // 忽略 EmailResponse
                .eraseToAnyPublisher()
        } else {
            return cdRequestCodeStep.execute(.init(phoneNumber: input.account))
                .map { _ in () }  // 忽略 SMSResponse
                .eraseToAnyPublisher()
        }
    }
}



struct CDGetSmsCodeStep: LoginStep {
    struct InputData {
        let phoneNumber: String
    }

    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.SMSSend.Response, Error> {
        Future<AuthAPI.SMSSend.Response, Error> { promise in
            let api = AuthAPI.SMSSend(body: .init(phoneNumber: input.phoneNumber))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                    case .success(let response):
                    if response.code as! Int == 200
                    {
                        Logger.log(.info, message: "获取验证码成功: \(response)",tag: .login)
                        promise(.success(response.data ?? AuthAPI.SMSSend.Response()))
                    }
                    else{
                        promise(.failure(NSError(domain: "获取验证码", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                    case .failure(let error):
                        promise(.failure(error))
                        Logger.log(.error, message: "获取验证码失败: \(error)", tag: .login)
                    }
            })
        }
        .eraseToAnyPublisher()
    }
    
}




struct CDGetEmailCodeStep: LoginStep {
    struct InputData {
        let email: String
        let type: RequestCodeType
    }
    
    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.EmailCodeSend.Response, Error> {
        Future<AuthAPI.EmailCodeSend.Response, Error> { promise in
            let api = AuthAPI.EmailCodeSend(body: .init(email: input.email, type: input.type))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                    case .success(let response):
                    if response.code as! Int == 200
                    {
                        Logger.log(.info, message: "获取验证码成功: \(response)",tag: .login)
                        promise(.success(response.data ?? AuthAPI.EmailCodeSend.Response()))
                    }
                    else{
                        promise(.failure(NSError(domain: "获取验证码", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                    case .failure(let error):
                        promise(.failure(error))
                        Logger.log(.error, message: "获取验证码失败: \(error)", tag: .login)
                    }
            })
        }
        .eraseToAnyPublisher()
    }
}
